"use strict";
var MyUtils = require('../../util/utils')
var myUtils = new MyUtils()
var connection = require('../../constant/mysqlconnection').connection


// var deleteApplication = function (req, res) {
//     // 关联 app appversion patch 三张表
//     var updateSql = 'update application app , appversion version , patch ptch set ' +
//         'app.logic_delete_flag = 99 , version.logic_delete_flag = 99 , ptch.logic_delete_flag = 99 where ' +
//         'app.id = ? or version.application_id = ? and ptch.appversion_id in ' +
//         '( select tmp.id from (select id from appversion where application_id = ?) as tmp)'
//     var updateSqlParam = [
//         req.body.appId,
//         req.body.appId,
//         req.body.appId
//     ]
//     console.log(updateSql)
//     connection.query(updateSql, updateSqlParam, function (error, result) {
//         if (!error) {
//             var bizSucModel = myUtils.generateSuccess()
//             if (result.affectedRows >= 1) {
//                 // 逻辑删除成功
//                 bizSucModel.model = {
//                     success: 0,
//                     info: '删除成功'
//                 }
//             } else {
//                 bizSucModel.model = {
//                     success: 1,
//                     info: '无效删除（未找到对应的应用程序）'
//                 }
//             }
//             res.end(JSON.stringify(bizSucModel))
//         } else {
//             console.log(error)
//             res.end(JSON.stringify(myUtils.generateInnerError()))
//         }
//     })
// }


function DeleteAction() {

    this.deleteApp = function (req, res) {
        var updateSql = 'update application set logic_delete_flag = 99 where id = ?'
        var updateSqlParams = [req.body.appId]
        var deleteAversion = this.deleteAppVersion
        connection.query(updateSql, updateSqlParams, function (error, results) {
            if (!error) {
                if (results.affectedRows >= 1) {
                    // 说明app 删除成功
                    deleteAversion(req,res)
                } else {
                    // 说明app 无效
                    var bizSucModel = myUtils.generateSuccess()
                    bizSucModel.model = {
                        success: 1,
                        info: '无效删除（未找到对应的应用程序）'
                    }
                    res.end(JSON.stringify(bizSucModel))
                }
            } else {
                res.end(JSON.stringify(myUtils.generateInnerError()))
            }
        })
    }

    this.deleteAppVersion = function (req, res) {
        var updateSql = ''
        var updateSqlParams = []
        var bizSucModel = myUtils.generateSuccess()
        if (req.body.appId) {
            // caller from deleteApp
            updateSql = 'update appversion set logic_delete_flag = 99 where application_id = ?'
            updateSqlParams[0] = req.body.appId
            bizSucModel.model = {
                success: 0,
                info: '删除成功'
            }
        } else {
            updateSql = 'update appversion set logic_delete_flag = 99 where id = ?'
            updateSqlParams[0] = req.body.appVersionId
            bizSucModel.model = {
                success: 1,
                info: '无效删除（未找到对应的版本号）'
            }
        }
        connection.query(updateSql, updateSqlParams, function (error, results) {
            if (!error) {
                if (results.affectedRows >= 1) {
                    // 删除补丁
                    deletePatch(req, res)
                } else {
                    // 版本号不存在
                    res.end(JSON.stringify(bizSucModel))
                }
            } else {
                res.end(JSON.stringify(myUtils.generateInnerError()))
            }
        })
    }

    function deletePatch(req, res) {
        var updateSql = ''
        var updateSqlParams = []
        if (req.body.appId) {
            // call from deleteApp
            //update patch set logic_delete_flag = '99' WHERE appversion_id in (select tmp.id from (select id from appversion where application_id = '123') as tmp)
            updateSql = 'update patch set logic_delete_flag = 99 where appversion_id in (select tmp.id from (select id from appversion where application_id = ?) as tmp)'
            updateSqlParams[0] = req.body.appId
        } else {
            // call from deleteAppVersion
            updateSql = 'update patch set logic_delete_flag = 99 where appversion_id = ?'
            updateSqlParams[0] = req.body.appVersionId
        }
        connection.query(updateSql, updateSqlParams, function (error, results) {
            if (!error) {
                // 不管是否存在补丁 都认为删除成功
                var bizSucModel = myUtils.generateSuccess()
                bizSucModel.model = {
                    success: 0,
                    info: '删除成功'
                }
                res.end(JSON.stringify(bizSucModel))
            } else {
                res.end(JSON.stringify(myUtils.generateInnerError()))
            }
        })
    }
}


module.exports = DeleteAction


